<?php
	//error_reporting(0);
	set_time_limit(E_ALL);
	include 'simplehtmldom/simple_html_dom.php';
	include_once(dirname(__FILE__).'/dbutil.php');
	function saveRecord($table,$record,$db){
    	if(empty($table))
    		return false;
    	if(!is_array($record) || empty($record))
    		return false;
    	//$db = new DBUtil();
    	//$this->initDB();
    	$r = array();
    	foreach($record as $key=>$item){
    		$r[$db->escape($key)] = $db->escape($item);
    	}
    	$cols = implode("," , array_keys($r));
    	$values = implode("','" , $r);
    	$sql = "insert into " . $db->escape($table) . " (" . $cols . ") values ('" . $values . "')";
    	return $db->query($sql) ? $db->getlastid() : false;
    }
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	// 房天下
/*	$baseUrl = 'http://newhouse.macau.fang.com';
	$url = $baseUrl . '/house/s/';*/
	//$url = 'http://newhouse.wuzhong.fang.com/house/s/b94/';
	$url='http://newhouse.sh.fang.com/house/s/';
	while(true){
		$recordList = array();
		// 西安房天下
		$listPage = file_get_html('compress.zlib://'.$url);
		//echo($listPage);exit;
		//$listPage = file_get_contents('compress.zlib://http://newhouse.xian.fang.com/house/dianshang/');
		//echo $listPage
		if(empty($listPage)){
			break;
		}
	//	$list = $listPage->find('div[class=nlc_details]');
		$list = $listPage->find('a[class=snblue]');
		
		if(!empty($list)){
			foreach($list as $listItem){
			//	$tmp = $listItem->find('div[class=nlcd_name]')[0]->find('a');
				$tmp=trim(mb_convert_encoding($listItem->innertext,'utf-8','gbk'));
				if($tmp == '本月开盘'){
				//	echo $tmp.'     '.$listItem->href.'<br>';
					$url=$listItem->href;
					$new=file_get_html('compress.zlib://'.$url);
					$nlist=$new->find();//本月开盘总数
					
				}
				if($tmp == '本月入住'){
				
				}
			/*	if(empty($tmp)){
					echo 1;exit;
					continue;
				}
				//有公开数据的城市链接，名字
				//$cityArray=array();
				for($a=1;$a<count($tmp);$a++){
					$td=$tmp[$a]->find('td');
					foreach($td as $md){
						$z = trim(mb_convert_encoding($md->innertext,'utf-8','gbk'));
						echo $z;
					}
						echo '<br>';
				
					
					if($tmp[$a]->href !== '/creisdata/'){
						$name = trim(mb_convert_encoding($tmp[$a]->innertext,'utf-8','gbk'));
						$qz="http://fdc.fang.com";
						echo $name.":".$qz.$tmp[$a]->href.'<br>';
						$cityArray[$a]=array('city_name'=>$name,'city_link'=>$qz.$tmp[$a]->href);
					}*/
				}
				
				/*
				$name = trim(mb_convert_encoding($tmp[0]->innertext,'utf-8','gbk'));
				$tmp = $listItem->find('div[class=address]');
				// 发现部分广告位只有名称，没有地址，跳过这些数据 2017/11/27
				if(empty($tmp)){
					continue;
				}
				$tmp = $tmp[0]->find('a');
			/*	$adress = trim(mb_convert_encoding($tmp[0]->title,'utf-8','gbk'));
				$record = array();
				$record['house_name'] = $name ;
				$record['address'] = $adress;
				//$record['city'] = 'fuzhou';
				//状态
				$record['status'] = 0;
				$recordList[] = $record;
				//break;*/
		//	}
			exit;
			//var_dump($recordList);exit;
			$db = new DBUtil();
			foreach($recordList as $item){
				$h = $db->getOneRow("select id from t_house where house_name = '".$db->escape($item['house_name'])."'");
				if(empty($h)){
					// 保存楼盘信息
					saveRecord('t_house',$item,$db);
				}
			}
			$db->close();
		}
		$nextPage = $listPage->find('div[class=page]')[0]->find('a[class=active]')[0]->next_sibling();
		echo ($nextPage->href.'<br/>');
		if(!empty($nextPage) ){
			$url = $baseUrl . $nextPage->href;
		}else{
			break;
		}
		//sleep(30);
		//echo $url;break;
	}
	
	// 删除html
	function removeHtml($value){
		return preg_replace('/<.*?<\/\w+>/','',$value);
	}
	// 删除全部空格
	function removeSpace($value){
		return preg_replace('/\s+/','',$value);
	}
?>